package com.telchina.workorder.app.controller;

import cn.hutool.crypto.digest.MD5;
import com.telchina.common.core.controller.BaseController;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.core.domain.entity.SysUser;
import com.telchina.common.core.domain.model.LoginUser;
import com.telchina.common.exception.ServiceException;
import com.telchina.common.utils.sign.Md5Utils;
import com.telchina.framework.security.service.SysPermissionService;
import com.telchina.framework.security.service.TokenService;
import com.telchina.framework.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "移动端接口v1")
@RestController
@RequestMapping("/profile/v1/app")
public class AppLoginController extends BaseController {


    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysPermissionService permissionService;


    @ApiOperation("用户登录")
    @GetMapping("appLogin")
    public AjaxResult appLogin(String loginName, String password) {
        SysUser user = this.userService.selectUserByUserName(loginName);

        if (!user.getLoginPwd().equals(password)) {
            throw new ServiceException("登录失败。");
        }
        LoginUser loginUser = new LoginUser(user, permissionService.getMenuPermission(user));
        loginUser.setDeptId(loginUser.getUser().getOrg().getId());
        loginUser.setUserId(loginUser.getUser().getId());
        Map<String, Object> result = new HashMap<>();
        user.setLoginPwd(null);
        result.put("user", user);
        result.put("token", tokenService.createToken(loginUser));
        return AjaxResult.success(result);
    }
}
